package cn.new18.testactivitytask.activity;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.new18.testactivitytask.R;
import cn.new18.testactivitytask.utils.MyRect;

/**
 * Created by RaoWei on 2016/12/22 17:08.
 */

public class E extends Activity {
    private static final String TAG = "E_A";
    @BindView(R.id.i_am_drag_view)
    FrameLayout mIAmDragView;
    @BindView(R.id.center_color)
    ImageView mCenter;
    @BindView(R.id.text)
    TextView mText;
    private int mHeight;
    private int mWidth;
    private int mScreenWidth;
    private int mScreenHight;
    private int mStatusBarHeight;
    private MyRect mCenterRect;
    private MyRect mImgRect;
    private MyRect mMoveArea;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.e);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        mScreenWidth = getResources().getDisplayMetrics().widthPixels;
        mScreenHight = getResources().getDisplayMetrics().heightPixels;
        mStatusBarHeight = getStatusBarHeight();
        mIAmDragView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mHeight = mIAmDragView.getHeight();
                mWidth = mIAmDragView.getWidth();
                mCenterRect = new MyRect(mCenter.getLeft(), mCenter.getTop(), mCenter.getRight(), mCenter.getBottom());
                Log.e(TAG + "+++", mCenter.getLeft() + ", " + mCenter.getTop() + ", " + mCenter.getRight() + ", " + mCenter.getBottom());

            }
        });
        mImgRect = new MyRect();
        mMoveArea = new MyRect(0, mStatusBarHeight, mScreenWidth, mScreenHight - mStatusBarHeight - 200);

        mIAmDragView.setOnTouchListener(new View.OnTouchListener() {
            float lastX;
            float lastY;

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int left = view.getLeft();
                int right = view.getRight();
                int top = view.getTop();
                int bottom = view.getBottom();
                float moveX;
                float moveY;
                float diffX;
                float diffY;
                Log.e(TAG, "left = " + left + ", right = " + right + "\ntop = " + top + ", bottom = " + bottom);
                Log.e(TAG, "width = " + mWidth + ", height = " + mHeight);
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = motionEvent.getRawX();
                        lastY = motionEvent.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        moveX = motionEvent.getRawX();
                        moveY = motionEvent.getRawY();
                        motionEvent.getX();
                        Log.e(TAG, "getX = " + motionEvent.getX() + ", getY = " + motionEvent.getY());
                        Log.e(TAG, "moveX = " + moveX + ", moveY = " + moveY);
                        Log.e(TAG, "view.getX = " + view.getX() + ", view.getY = " + view.getY());
                        diffX = moveX - lastX;
                        diffY = moveY - lastY;
                        lastX = moveX;
                        lastY = moveY;
                        int l = (int) (left + diffX);
                        int t = (int) (top + diffY);
                        int r = (int) (right + diffX);
                        int b = (int) (bottom + diffY);
                        Log.e(TAG + "+++", l + ", " + t + ", " + r + ", " + b);
                        mImgRect.set(l, t, r, b);
                        Log.e(TAG + "===", mMoveArea.toString());
                        Log.e(TAG + "===", mImgRect.toString());
                        Log.e(TAG + "===", mScreenWidth + ", "+ mScreenHight + ", " + mStatusBarHeight);
                        if (mMoveArea.contains(mImgRect)) {
                            //正常情况
                            view.layout(l, t - mStatusBarHeight, r, b - mStatusBarHeight);
                        } else {
                        //到达不同边界
                            Log.e(TAG + "===", "走了else");
                            if (left <= 0 || right >= mScreenWidth) {
                                view.offsetTopAndBottom((int) diffY);
                            }
                            if (top <= 0 || bottom >= mScreenHight) {
                                view.offsetLeftAndRight((int) diffX);
                            }
                        }
                        mText.setText("null");
                        Log.e(TAG + "===", "mMoveArea.contains(mImgRect) = " + mMoveArea.contains(mImgRect) +
                                "\n mMoveArea.intersect(mImgRect) = " + mMoveArea.intersect(mImgRect) +
                                "\n mMoveArea.equals(mImgRect)" + mMoveArea.equals(mImgRect));
                        break;
                }
                return true;
            }
        });

        //mIAmDragView.layout();
    }

    private int getStatusBarHeight() {
        Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        Log.e(TAG, "status bar height:" + rect.top);
        return rect.top;
    }
}
